﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace MDIDemo.Windows.Report
{
    public partial class frmBaoCaoNKGiaoDichTheoNgay : DevExpress.XtraEditors.XtraForm
    {
        public string sErr = "";
        Dictionary<string, object> _valueList = new Dictionary<string, object>();

        string _type = "BKGDCTTN";

        public string Type
        {
            get { return _type; }
            set { _type = value; }
        }
        public Dictionary<string, object> ValueList
        {
            get { return _valueList; }
            set { _valueList = value; }
        }
        DataSet _ds = null;
        public DataSet DataSource { get { return _ds; } }


        public frmBaoCaoNKGiaoDichTheoNgay()
        {
            InitializeComponent();
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            Close();
        }

        public DataSet BuildDataSourceNKGiaoDich()
        {
            DataSet _ds = null;
            string query = "";

            query = @"SELECT     SOCT, LOAICT, NGAYCT, MAKH,     TONGTIEN, TIENMAT,  THUNGAY,  NameKH, DIACHIKH,  
                      NOIDUNG,
                          (SELECT     TENGD
                            FROM          CUSTINFO AS b
                            WHERE      (a.MAKH = CUSTID)) AS TENKH
FROM         GIAODICH AS a
WHERE     (NGAYCT >= '{0:yyyy-MM-dd}') and (NGAYCT <= '{1:yyyy-MM-dd}') and (LOAICT='PT001' or LOAICT='PC001')";
            query = string.Format(query, txtDateF.EditValue, txtDateT.EditValue);


            BUS.CommonControl ctr = new BUS.CommonControl();
            _ds = ctr.executeSelectQueries(query);

            query = @"SELECT 	
(select SUM(TIENMAT) FROM  dbo.[GIAODICH]
WHERE NGAYCT < '{0:yyyy-MM-dd}' and LOAICT = 'PC001' ) as TIENCHI,
(select SUM(TIENMAT) FROM  dbo.[GIAODICH]
WHERE NGAYCT < '{0:yyyy-MM-dd}' and LOAICT = 'PT001') as TIENTHU";
            query = string.Format(query, txtDateF.EditValue, txtDateT.EditValue);

            DataTable dt = ctr.executeSelectQuery(query);
            if (_ds.Tables[0].Rows.Count > 0)
            {
                dt.TableName = "Table1";
                _ds.Tables.Add(dt.Copy());
            }


            return _ds;

        }
        private void btnPrint_Click(object sender, EventArgs e)
        {
            _ds = BuildDataSourceNKGiaoDich();

            if (_ds.Tables.Count == 0)
            {
                XtraMessageBox.Show("Dữ liệu không tồn tại!");
                return;
            }
            bool isdata = false;
            foreach (DataTable dt in _ds.Tables)
            {
                if (dt.Rows.Count > 0)
                {
                    isdata = true;
                    break;
                }
            }
            if (isdata == false)
            {
                XtraMessageBox.Show("Dữ liệu không tồn tại!");
                return;
            }
            _valueList.Add("DateF", txtDateF.EditValue);
            _valueList.Add("DateT", txtDateT.EditValue);
            DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }

        private void frmBangKeGTGT_Load(object sender, EventArgs e)
        {
            txtDateF.EditValue = new DateTime(frmMain.__ngayMoSo.Year, frmMain.__ngayMoSo.Month, 1);
            txtDateT.EditValue = frmMain.__ngayMoSo;
        }
    }
}